﻿@using Think9.Models;
@model Think9.Models.RelationRDEntity
@{ ViewBag.Title = "Add";
    Layout = "~/Areas/Shared/_LayuiForm.cshtml"; }

<form class="layui-form" lay-filter="formUser">
    <input style="display:none" id="Guid" value="@ViewBag.Guid">
    <input style="display:none" id="tbid" value="@ViewBag.tbid">
    <input style="display:none" id="id" value="@ViewBag.id">
    <div class="layui-form-item">
        <label class="layui-form-label">名 称</label>
        <div class="layui-input-block">
            <input type="text" name="RelationName" placeholder="必填 请输入名称" autocomplete="off" class="layui-input" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">读取录入表</label>
        <div class="layui-input-block">
            <input type="text" name="FromTbId" id="FromTbId" style="display:none" value="@Model.FromTbId">
            <input type="text" name="FromTbName" id="FromTbName" autocomplete="off" class="layui-input" readonly="readonly">
        </div>
    </div>

    <fieldset class="table-fieldset">
        <legend>读取字段</legend>
        <div class="layui-form-item">
            <label class="layui-form-label">数据源字段</label>
            <div class="layui-input-block">
                <select id="SelectIndexId" name="SelectIndexId">
                    <option value="">==请选择 需先选择读取录入表==</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">目标字段</label>
            <div class="layui-input-block">
                <select id="FillIndexId" name="FillIndexId">
                    @foreach (valueTextEntity item in ((IEnumerable<valueTextEntity>)ViewBag.FillIndexId))
                    {
                        <option value="@item.Value">@item.Text</option>
                    }
                </select>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: right;">
            点击【添加读取字段】按钮可添加多个读取字段>>
            <button type="button" class="layui-btn " id="addListFiled"><i class="fa fa-plus"></i>添加读取字段</button>
        </div>
        <div class="layui-form-item">
            <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
        </div>
    </fieldset>
    <br />

    <div class="layui-form-item">
        <div class="layui-row">
            <div class="layui-col-md10">
                <label class="layui-form-label">排序字段</label>
                <div class="layui-input-block">
                    <select id="OrderIndexId" name="OrderIndexId">
                        <option value="">==请选择 需先选择读取录入表==</option>
                    </select>
                </div>
            </div>
            <div class="layui-col-md2" align="left">
                <div class="layui-input-block">
                    <input type="hidden" name="OrderType" value="1">
                    <input type="checkbox" id="OrderType" name="OrderType" value="2" title="倒序">
                </div>
            </div>
        </div>
    </div>

    <br />

    <fieldset class="table-fieldset">
        <legend>筛选条件</legend>
        <div class="layui-form-item">
            <label class="layui-form-label">连 接</label>
            <div class="layui-input-inline">
                <select name="linkstr" id="linkstr">
                    <option value=""></option>
                    <option value=" AND "> 并且 ( AND )</option>
                    <option value=" OR "> 或者 ( OR )</option>
                </select>
            </div>
            <label class="layui-form-label">判 断</label>
            <div class="layui-input-inline">
                <select name="judge" id="judge">
                    <option value="=">等于</option>
                    <option value=">">大于</option>
                    <option value=">=">大于等于</option>
                    <option value="<">小于</option>
                    <option value="<=">小于等于</option>
                    <option value="<>">不等于</option>
                    <option value="like">包含</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字 段</label>
            <div class="layui-input-block">
                <select id="ConditionFiled" name="ConditionFiled">
                    <option value="">==请选择 需先选择取值方式==</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">值</label>
            <div class="layui-input-inline">
                <input type="text" name="valuekey" id="valuekey" placeholder="输入或选择" class="layui-input">
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-primary" id="selectvalue"><i class="fa fa-check"></i>选择</button>
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn " id="addvalue"><i class="fa fa-plus"></i>添加筛选条件</button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">筛选条件</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" id="WhereStr1" name="WhereStr1"></textarea>
            </div>
        </div>
    </fieldset>

    <br />
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="edit">编辑</button>
        </div>
    </div>
</form>
<script>
    layui.use(["table", "form", "exLayer", "exUtils", "layer"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let tableSelect = layui.tableSelect;
        let layer = layui.layer;

        let $ = layui.$;

        form.render(); //初始化表单，要加上，不然刷新部分组件可能会不加载

        getVal();

        form.val("formUser", {
            "RelationName": "@Model.RelationName"
            , "FromTbId": "@Model.FromTbId"
            , "FromTbName": "@Model.FromTbName"
            , "OrderIndexId": "@Model.OrderIndexId"
            , "WhereStr1": "@Html.Raw(@Model.WhereStr1)"
        });
        function getVal() {
            if (@Model.OrderType == "2") {
                $('#OrderType').attr("checked", true);
            }
            else {
                $("#OrderType").removeAttr("checked");
            }
        }

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysTable/TbRelation/GetListFiled?guid=" + $("#Guid").val() + "&tbid=" + $("#tbid").val() + "&fromtbid=" + $("#FromTbId").val() + "&id=" + $("#id").val(),
            page: false,
            method: "POST",
            cols: [[
                { field: "Info1", title: "源字段" },
                { field: "Info2", title: "目标字段" },
                { title: "操作", width: 120, align: "center", fixed: "right", templet: "#operationTpl" }
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "del":
                    del(data.id);
                    break;
            }
        });

        GetFieldList($("#FromTbId").val());

        form.on('select(FromTbId)', function (data) {
            GetFieldList(data.value);
        })

        form.on("submit(edit)", function (data) {
            exUtils.ajax("/SysTable/TbRelation/EditR1", "post", { model: data.field, id: $("#id").val(), tbid: $("#tbid").val() }, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                    parent.layui.table.reload('tableId', { url: "/SysTable/TbRelation/GetList?tbid=" + $(window.parent.document).find("#tbid").val() });
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        $('#selectvalue').on('click', function () {
            exLayer.openMiddle('选择', '/SysTable/TbRelation/SelectValue?tbid=' + $("#tbid").val(), '800px', '400px', null, null);
        });

        $('#addListFiled').on('click', function () {
            var _value = $("#SelectIndexId").val();
            var _value2 = $("#FillIndexId").val();
            if (_value == '' || _value2 == '') {
                exLayer.yellowSighMsg("字段无效");
                return false;
            }
            else {
                exUtils.ajax("/SysTable/TbRelation/AddListFiled", "post", { guid: $("#Guid").val(), id: $("#id").val(), value: $("#SelectIndexId").val(), value2: $("#FillIndexId").val() }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    ThisTable.reload({
                        url: "/SysTable/TbRelation/GetListFiled?guid=" + $("#Guid").val() + "&tbid=" + $("#tbid").val() + "&fromtbid=" + $("#FromTbId").val() + "&id=" + $("#id").val()
                    });
                }).fail(function (error) {
                    console.log(error);
                });
            }
        });

        $('#addvalue').on('click', function () {
            var _old = $("#WhereStr1").val();
            var _new = _old + ' ' + $("#linkstr").val() + ' ' + $("#ConditionFiled").val() + ' ' + $("#judge").val() + ' ' + $("#valuekey").val();
            $("#WhereStr1").val(_new);

            $("#valuekey").val('');
        });

        function GetFieldList(id) {
            GetSelectValueFieldByTb(id);
            GetSelectOrderFieldByTb(id);
            GetConditionFieldListByTb(id);
        }

        function GetSelectValueFieldByTb(id) {
            var controlid = "#SelectIndexId";
            $("" + controlid + "").empty(); //清空控件
            $.getJSON("/SysTable/TbRelation/GetSelectValueFieldByTb", { id: id }, function (data) {
                $.each(data, function (i, item) {
                    $("" + controlid + "").append(new Option(item.Text, item.Value));
                });
                form.render();
            });
        }

        function GetSelectOrderFieldByTb(id) {
            var controlid = "#OrderIndexId";
            $("" + controlid + "").empty(); //清空控件
            $.getJSON("/SysTable/TbRelation/GetSelectOrderFieldByTbId", { id: id }, function (data) {
                $.each(data, function (i, item) {
                    $("" + controlid + "").append(new Option(item.Text, item.Value));
                });
                form.render();
            });
        }

        function GetConditionFieldListByTb(id) {
            var controlid = "#ConditionFiled";
            $("" + controlid + "").empty(); //清空控件
            $.getJSON("/SysTable/TbRelation/GetConditionFieldList", { id: id }, function (data) {
                $.each(data, function (i, item) {
                    $("" + controlid + "").append(new Option(item.Text, item.Value));
                });
                form.render();//一定要加并且只能加在此处 否则下拉框不正确显示
            });
        }

        function del(id) {
            exUtils.ajax("/SysTable/TbRelation/DeleteFiled", "get", { id: id, guid: $("#Guid").val()}, true).done(function (response) {
                ThisTable.reload({
                    url: "/SysTable/TbRelation/GetListFiled?guid=" + $("#Guid").val() + "&tbid=" + $("#tbid").val() + "&fromtbid=" + $("#FromTbId").val() + "&id=" + $("#id").val()
                });
            }).fail(function (error) {
                console.log(error);
            });
        }
    });
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" id="del">删除</a>
</script>